
import 'package:amway_superapp_creator_plugin/config/colors_config.dart';
import 'package:amway_superapp_creator_plugin/routers/application.dart';
import 'package:amway_superapp_creator_plugin/routers/routes.dart';
import 'package:amway_superapp_creator_plugin/viewmodels/home/home_guide_list_viewmodel.dart';
import 'package:amway_superapp_creator_plugin/views/base_view.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:provider/provider.dart';

import '../../view_state.dart';

const imgUrl =
    "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1602587327547&di=aae4274fb065ad4fe013348eac75f67a&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F52%2F52%2F01200000169026136208529565374.jpg";

class GuideListWidget extends StatelessWidget {
  var scrollController = new ScrollController();
  bool isScroll;

  GuideListWidget(this.isScroll);

  @override
  Widget build(BuildContext context) {
    return BaseView(
      model: HomeGuideListViewModel(api: Provider.of(context)),
      onModelReady: (model) {
        model.queryGuideDatas(isScroll);

      },
      builder: (context, model, child) => Container(

        child: model.state == ViewState.Busy
            ? Center(
                child: CircularProgressIndicator(),
              )
            : ListView.builder(
                shrinkWrap: true,
                physics: isScroll ? null : NeverScrollableScrollPhysics(),
                controller: scrollController,
                itemCount: model.datas.length,
                itemBuilder: (context, index) {
                  return _ListWidget(context, model, index);
                }),
      ),
    );
  }
}

Widget _ListWidget(context, HomeGuideListViewModel model, index) {
  return InkWell(
      onTap: () {
        Map<String, dynamic> parameters = {};
        parameters['dataHtml'] = model.datas[index].content;
        parameters['type']=0;
        Application.router.navigateTo(context, Routes.GUIDEDETAILS,routeSettings: RouteSettings(arguments: parameters));


      },
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
            bottom: BorderSide(width: 0.1, color: ColorsConfig.c_A6A6A6),
          ),
        ),
        padding: EdgeInsets.only(
            top: ScreenUtil().setHeight(14),
            bottom: ScreenUtil().setHeight(14),
            left: ScreenUtil().setWidth(20),
            right: ScreenUtil().setWidth(20)),
        child: Row(
          children: <Widget>[
            _GuideImage(context, model, index),
            Column(
              children: <Widget>[
                _GuideName(context, model, index),
              ],
            ),
          ],
        ),
      ));
}

//指南图片
Widget _GuideImage(context, HomeGuideListViewModel model, index) {
  return Container(
    width: ScreenUtil().setWidth(67.0),
    height: ScreenUtil().setHeight(52.0),
    //Image.network(newList[index].image),
    child: ClipRRect(
      borderRadius: BorderRadius.circular(4),
      child: FadeInImage.assetNetwork(
        image: " ",
        fit: BoxFit.cover,
        imageErrorBuilder: (context, error, stackTrace) =>
            Image.asset(
              'picture/img_defa_guide.png',
              package: 'amway_superapp_creator_plugin',
            ),
        placeholder: 'picture/img_defa_head.png',
        placeholderErrorBuilder: (context, error, stackTrace) =>
            Image.asset(
              'picture/img_defa_guide.png',
              package: 'amway_superapp_creator_plugin',
            ),
      ),
    ),
  );
}

//指南标题
Widget _GuideName(context, HomeGuideListViewModel model, index) {
  return Container(
    margin: EdgeInsets.only(left: ScreenUtil().setWidth(14)),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Container(
          alignment: Alignment.centerLeft,
          child: Text(
            model.datas[index].title,
            style: TextStyle(
                fontSize: ScreenUtil().setSp(18.0),
                color: ColorsConfig.c_2c2c2c),
          ),
        ),
        Container(
          margin: EdgeInsets.only(top: ScreenUtil().setHeight(12)),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                child: Image.asset(
                  "picture/icon_clock.png",
                  package: 'amway_superapp_creator_plugin',
                ),
              ),
              Container(
                child: Text(
                  DateUtil.formatDateStr(model.datas[index].publishDate,
                      format: "yyyy-MM-dd"),
                  style: TextStyle(
                    color: ColorsConfig.c_802c2c2c,
                    fontSize: ScreenUtil().setSp(12),
                  ),
                ),
              ),
             /* Container(
                margin: EdgeInsets.only(left: ScreenUtil().setWidth(16)),
                child: Image.asset(
                  "picture/icon_eyes.png",
                  package: 'amway_superapp_creator_plugin',
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: ScreenUtil().setWidth(3)),
                child: Text(
                  '${model.datas[index].views}',
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(12),
                      color: ColorsConfig.c_959595),
                ),
              )*/
            ],
          ),
        )
      ],
    ),
  );
}
